package com.rzgeo.wcgallay.model;

import com.rzgeo.rzmaplib.util.RmlUtil;
import com.rzgeo.wcgallay.util.WcmUtil;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by zhanghang on 2017/11/29.
 */

public class WcmSysFolder {
    public final File mainSysFolder;
    public final File datasFolder;
    public final File shareFolder;
    public final File screenShotFolder;
    public final File slideFolder;

    public WcmSysFolder(File mainSysFolder) {
        this.mainSysFolder = mainSysFolder;
        this.datasFolder = WcmUtil.Files.join(mainSysFolder,"data");
        this.shareFolder = WcmUtil.Files.join(mainSysFolder,"share");
        this.screenShotFolder = WcmUtil.Files.join(mainSysFolder,"screenshot");
        this.slideFolder = WcmUtil.Files.join(mainSysFolder,"slide");

        File[] files = new File[]{mainSysFolder,
                datasFolder,
                shareFolder,
                screenShotFolder,
                slideFolder
        };
        for(File f:files){
            if(!f.exists()){
                if(!f.mkdirs())
                    WcmUtil.Tip.Alert("创建系统文件夹失败，请同意所有权限，并重启程序。");
            }
        }
    }

    public WcmReseFolder[]listRese(){
        ArrayList<WcmReseFolder>rets=new ArrayList<>();
        for(File f:datasFolder.listFiles()){
            if(WcmReseFolder.isVailid(f)){
                try {
                    WcmReseFolder ref = new WcmReseFolder(f);
                    rets.add(ref);
                }catch (Exception e){
                    WcmUtil.Tip.Toast("数据文件夹中有不可识别的项目文件");
                }
            }
        }
        return rets.toArray(new WcmReseFolder[0]);
    }

    public File getGrobleLayer() {
        File dom = RmlUtil.Files.join(shareFolder, "dom");
        return RmlUtil.Files.join(dom, "天地图全球背景.tpk");
    }

    public static class WcmReseFolder{

        public final File dirFolder;
        private File shareDir;
        private File shareDom;
        private File shareDem;

        private WcmPart[] _wcmParts;

        public WcmReseFolder(File dirFolder){
            this.dirFolder = dirFolder;

            this.shareDom=null;

            ArrayList<WcmPart>wcmPartArr=new ArrayList<>();

            for(File f:dirFolder.listFiles()){
                if(f.isDirectory()){
                    if(f.getName().equalsIgnoreCase("共用"))
                        shareDir=f;
                    else
                        wcmPartArr.add(new WcmPart(f));
                }
            }
            _wcmParts=wcmPartArr.toArray(new WcmPart[0]);

            if(shareDir!=null){
                for(File f:shareDir.listFiles()){
                    if(f.isFile()){
                        if(f.getName().toLowerCase().endsWith(".tpk"))
                            shareDom=f;
                        else if(f.getName().toLowerCase().endsWith(".tif"))
                            shareDem=f;
                    }
                }
            }
        }


        public static boolean isVailid(File f) {

            return f.exists()&&f.isDirectory();
        }

        public String getName() {
            return dirFolder.getName();
        }

        public String getSnapPath() {
            return "";
        }

        public File getShareDom() {
            return shareDom;
        }

        public File getShareDem() {
            return shareDem;
        }

        public WcmPart[]getWcmParts(){
            return _wcmParts;
        }

        public static class WcmPart{

            public final File dirFolder;
            public final File[] exRasterPaths;
            public final File[] domPaths;
            public final WcmDemWithColors[] demPaths;
            public File vecGdbFile;

            public float currentElv=0;
            public float currentVolume=0;
            public int iCurrentInfo=-1;


            public WcmPart(File dirFolder) {
                this.dirFolder = dirFolder;


                File subFiles=WcmUtil.Files.join(dirFolder,"raster");
                this.exRasterPaths = subFiles.listFiles();

                subFiles=WcmUtil.Files.join(dirFolder,"dom");
                this.domPaths = subFiles.listFiles();

                subFiles=WcmUtil.Files.join(dirFolder,"dem");

                ArrayList<WcmDemWithColors>files=new ArrayList<>();
                for(File colorPng:subFiles.listFiles()){
                    try {

                        if(colorPng.isFile()&&
                                colorPng.getName().toLowerCase().endsWith(".png")){
                            File tpk=WcmUtil.Files.join(
                                    subFiles,
                                    RmlUtil.Files.fileName(colorPng)+".tpk");

                            if(tpk.exists()&&tpk.isFile())
                                files.add(new WcmDemWithColors(colorPng,tpk));
                        }
                    }catch (Exception e){

                    }

                }
                this.demPaths=files.toArray(new WcmDemWithColors[0]);

                // /storage/emulated/0/WCM/data/XXSK/vec.geodatabase
                File gdb = WcmUtil.Files.join(dirFolder, "vec.geodatabase");
                if(gdb.exists()&&gdb.isFile())
                    this.vecGdbFile = gdb;
            }

            public String getName() {
                return dirFolder.getName();
            }

            public String getSnapPath() {
                return "";
            }
        }

        public static class WcmDemWithColors{
            public final File ColorBeltLegend;
            public final File DemTpk;

            public WcmDemWithColors(File colorBeltLegend, File demTpk) {
                ColorBeltLegend = colorBeltLegend;
                DemTpk = demTpk;
            }
        }
    }
}
